#include <stdio.h>

#include "include/vehicle.h"
#include "include/stn.h"
#include "include/sysutils.h"
#include "include/filesystem.h"


unsigned char CharToHex(unsigned char bHex)
{
	if( bHex <= 9 )
		bHex += 0x30;
	else if( (bHex >= 10) && (bHex <= 15) )
		bHex += 0x37;
	else bHex = 0xFF;
	return bHex;
}

unsigned char HexToChar(unsigned char bChar)
{
	if( (bChar >= 0x30) && (bChar <= 0x39) )
		bChar -= 0x30;
	else if ( (bChar >= 0x41) && (bChar <= 0x46) )
		bChar -= 0x37;
	else if ( (bChar >= 0x61) && (bChar <= 0x66) )
		bChar -= 0x57;
	else bChar = 0xFF;
	return bChar;
}

/*  从文件获得日期时间，返回日期时间的字符数 */
int GetDateTimeFromFile(unsigned int * data)
{
	FILE * tfile;
	unsigned char datetime[TIME_FILE_MAX_LEN];
	long index  = 0;
	if( (tfile = fopen(TIME_FILE, "r")) == NULL){
		return TIME_FILE_NOT_EXIST;
	}

	for(index = 0; index < TIME_FILE_MAX_LEN; index++)
	{
		datetime[index] = fgetc(tfile);
		if(datetime[index] == 0x0D) break;
		}
    fclose(tfile);
    
	if(index != TIME_FILE_MAX_LEN - 2)
	{
		remove(TIME_FILE);
		return TIME_FILE_NOT_EXIST;
		}

	datetime[index] = 0;
		
	data[0] = HexToChar(datetime[0]) * 1000  \
			+ HexToChar(datetime[1]) * 100 \
			+ HexToChar(datetime[2]) * 10  \
			+ HexToChar(datetime[3]);
	data[0] -=2000;
	data[1] = HexToChar(datetime[5]) * 10 + HexToChar(datetime[6]);
	data[2] = HexToChar(datetime[8]) * 10 + HexToChar(datetime[9]);
	data[3] = HexToChar(datetime[11]) * 10 + HexToChar(datetime[12]);
	data[4] = HexToChar(datetime[14]) * 10 + HexToChar(datetime[15]);
	data[5] = HexToChar(datetime[17]) * 10 + HexToChar(datetime[18]);

	printf("%s(%d)\n", datetime, index);
	printf("%d,%d,%d,%d,%d,%d\n",data[0],data[1],data[2],data[3],data[4],data[5]);
	remove(TIME_FILE);
	
	return index;
}

void SaveFile(FILE* file, unsigned char *data, int length)
{
	int index;
	for(index = 0; index < length; index++)
	{
		fputc(CharToHex((data[index] & 0xF0) >> 4), file);
		fputc(CharToHex(data[index] & 0x0F), file);
	}
} 

long GetFileLength(FILE *file)
{
	long length;
	if(file == NULL) return -1;
	fseek(file, 0L, SEEK_END);
	length = 	ftell(file);
	fseek(file, 0L, SEEK_SET);
	return length;
}


int GetDataFromFile(FILE *file, unsigned char * data, int length)
{
	int index;
	unsigned char c1,c2;
	//printf("Get Data from file[%d]:", length);
	for(index = 0; index < length; index++){
		//if(index % 16 == 0) printf("\n");
		c1 = fgetc(file);
		c2 = fgetc(file);
		data[index] = HexToChar(c1)*16 + HexToChar(c2);
		//printf("%02X ", data[index]);
	}
	return index;
}

void AddLastRecordNumber(unsigned char *data, int num)
{
	unsigned char numChar[16];
	int index;
	memset(numChar, 0x20, 16);
	sprintf(numChar,"%015d", num);
	for(index = 0; index < 15; index++){
		data[CONFIG_FILE_ID_ADDR + index] = numChar[index];
	}
}

void AddLastRecordNumberToIndex(unsigned char *data, int num)
{
	unsigned char numChar[16];
	int index;
	memset(numChar, 0X20, 16);
	sprintf(numChar, "%015d", num);
	for(index = 0; index < 15; index++){
		//Index 文件中，最新的文件序号保存在后15位位置上，前15位为手机下载的文件序号
		data[index+ 15] = numChar[index];
	}
}

int GetLastRecordNumFromBuffer(unsigned char *data)
{
	unsigned char numChar[16];
	int index=0;
	int i;
	memset(numChar, 0, 16);
	for(index = 0; index < 15; index++){
		numChar[index] = data[CONFIG_FILE_ID_ADDR + index];
		if(numChar[index] == 0x20) return 0;
	}
	return atoi(numChar);
}


